package com.badlogic.androidgames.game;

import java.util.LinkedList;
import android.graphics.Rect;

import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Pixmap;

public class SimpleAnimation extends GameObject{
	
	int loop, currentLoop;
	float timer, time;
	int currentFrame;
	
	LinkedList<Rect> frames = new LinkedList<Rect>();
	
	public SimpleAnimation(Pixmap tex, Vector2 pos, Vector2 size, Vector2 srcSize, int loop)
	{
		super (tex, pos, size, new Vector2(0, 0), srcSize);
		
		this.loop = loop;
		this.currentLoop = 0;
		this.time = 0.2f;
		this.currentFrame = 0;
		
		CreateFrames();
	}
	
	public void CreateFrames()
	{
		frames.clear();
		Vector2 frameAmount = new Vector2((int)(tex.getWidth() / srcSize.x),
										  (int)(tex.getHeight() / srcSize.y));
		
		for (int y = 0; y < frameAmount.y; y++)
			for (int x = 0; x < frameAmount.x; x++)
			{
				frames.add(new Rect((int)(x * srcSize.x), 
								    (int)(y * srcSize.y),
								    (int)(x * srcSize.x) + (int)srcSize.x,
								    (int)(y * srcSize.y) + (int)srcSize.y));
									
			}
	}
	
	public void Update(float deltaTime)
	{
		timer += deltaTime;
		if (timer > time)
		{
			timer -= time;
			currentFrame++;
			
			if (currentFrame >= frames.size())
				currentFrame = 0;
			
			srcRect = frames.get(currentFrame);
		}
		super.Update();
	}
	
	public void Draw(Graphics g)
	{
		super.Draw(g);
	}
}
